/**
 * @bug 已经删除的动态，可以点赞和取消点赞，可以评论，还可以回复评论
 */

const express = require('express');
const router = express.Router();

// 实体类
const Response = require('../entity/http/Response');
const PreparePost = require('../entity/community/PreparePost');
const ObjId = require('../entity/common/ObjId');
const LikePost = require('../entity/community/LikePost');
const PostComment = require('../entity/community/PostComment');

// 业务层
const communityService = require('../service/communityService');

/**
 * @接口名称 获取动态类型接口
 */
router.get('/postTypes', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /postTypes 接口`);
    communityService.getPostTypes().then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("获取动态类型失败"));
    });
});

/**
 * @接口名称 预发布动态接口
 * @用途 申请发布带有图片的动态
 * @参数 { title: string, content: string, imgCount: number, type: string }
 */
router.post('/prepare-post', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /prepare-post 接口`);
    let uid = req.jwtResult.data.uid;
    let preparePost = new PreparePost(req.body);
    let flag = preparePost.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.preparePost(uid, preparePost).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("预发布动态失败"));
    });
});

/**
 * @接口名称 发布动态接口
 * @用途 标记预发布的动态为正式动态
 * @参数 { id: number }
 */
router.post('/finalize-post', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /finalize-post 接口`);
    let uid = req.jwtResult.data.uid;
    let post = new ObjId(req.body, 'id');
    let flag = post.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.finalizePost(uid, post.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("发布动态失败"));
    });
});

/**
 * @接口名称 标记图片上传完毕接口
 * @用途 将预发布动态拿到的图片预签名URL上传后 标记图片上传完毕 等待发布
 * @参数 { id: number }
 */
router.post('/img-uploaded', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /img-uploaded 接口`);
    let uid = req.jwtResult.data.uid;
    let img = new ObjId(req.body, 'id');
    let flag = img.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.finalizeImg(uid, img.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("标记图片失败"));
    });
});

/**
 * @接口名称 删除动态接口
 * @用途 删除动态
 * @参数 { id: number }
 */
router.delete('/post', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /post 接口`);
    let uid = req.jwtResult.data.uid;
    let post = new ObjId(req.body, 'id');
    let flag = post.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.delPost(uid, post.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("删除动态失败"));
    });
});

/**
 * @接口名称 获取推荐动态接口
 * @用途 获取推荐动态
 * @参数 ?lastPostId=number
 * @参数详情 lastPostId 为可选参数 用于懒加载接下来的动态
 */
router.get('/recommend-post', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /recommend-post 接口`);
    let uid = req.jwtResult.data.uid;
    let post = new ObjId(req.query, 'lastPostId', true);
    let flag = post.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.getRecommendPosts(uid, post.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("获取推荐动态失败"));
    });
});

/**
 * @接口名称 根据动态类型获取推荐动态接口
 * @参数 ?lastPostId=number&typeId=number
 * @参数详情 lastPostId 为可选参数 用于懒加载接下来的动态
 * @参数详情 typeId 动态类型ID
 */
router.get('/recommend-post-by-type', function (req, res) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /recommend-post-by-type 接口`);
    let uid = req.jwtResult.data.uid;
    let post = new ObjId(req.query, 'lastPostId', true);
    let flag = post.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    let type = new ObjId(req.query, 'typeId');
    flag = type.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    communityService.getRecommendPostsByType(uid, post.id, type.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("获取推荐动态失败"));
    });
});

/**
 * @接口名称 懒加载获取自己动态接口
 * @参数 ?lastPostId=number
 * @参数详情 lastPostId 为可选参数 用于懒加载接下来的动态
 */
router.get('/self-post', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /self-post 接口`);
    let uid = req.jwtResult.data.uid;
    let post = new ObjId(req.query, 'lastPostId', true);
    let flag = post.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.getSelfPosts(uid, post.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("获取动态失败"));
    });
});

/**
 * @接口名称 查看动态详情接口
 * @用途 查看动态详情 并且 记录一次查看量
 * @参数 ?id=number
 */
router.get('/post', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /post 接口`);
    let uid = req.jwtResult.data.uid;
    let post = new ObjId(req.query, 'id');
    let flag = post.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.getPost(uid, post.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("获取动态失败"));
    });
});

/**
 * @接口名称 点赞动态接口
 * @参数 { id: number, like: boolean }
 * @参数详情 like 为 true 表示点赞，为 false 表示取消点赞
 */
router.post('/like-post', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /like-post 接口`);
    let uid = req.jwtResult.data.uid;
    let post = new LikePost(req.body, 'id');
    let flag = post.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.likePost(post, uid).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("操作失败"));
    });
});

/**
 * @接口名称 发布一级评论接口
 * @参数 { id: number, content: string }
 * @参数详情 id: 动态id
 */
router.post('/post-comment', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /post-comment 接口`);
    let uid = req.jwtResult.data.uid;
    let comment = new PostComment(req.body);
    let flag = comment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.postComment(comment, uid).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("操作失败"));
    });
});

/**
 * @接口名称 发布二级评论接口
 * @参数 { id: number, content: string }
 * @参数详情 id: 一级评论id
 */
router.post('/post-comment-reply', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /post-comment-reply 接口`);
    let uid = req.jwtResult.data.uid;
    let comment = new PostComment(req.body);
    let flag = comment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    communityService.postCommentReply(comment, uid).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("操作失败"));
    });
});

/**
 * @接口名称 获取动态一级评论接口
 * @参数 ?id=number&lastCommentId=number
 */
router.get('/post-comment', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /post-comment 接口`);
    // 获取动态id
    let post = new ObjId(req.query, 'id');
    let flag = post.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    // 获取最后一条评论的id
    let lastComment = new ObjId(req.query, 'lastCommentId', true);
    flag = lastComment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 请求业务层
    communityService.getPostComment(post.id, lastComment.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("操作失败"));
    });
});

/**
 * @接口名称 获取动态二级评论接口
 * @参数 ?id=number&lastCommentId=number
 */
router.get('/post-comment-reply', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /post-comment-reply 接口`);
    // 获取评论id
    let postComment = new ObjId(req.query, 'id');
    let flag = postComment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    // 获取最后一条二级评论的id
    let lastComment = new ObjId(req.query, 'lastCommentId', true);
    flag = lastComment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    // 请求业务层
    communityService.getPostCommentReply(postComment.id, lastComment.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("操作失败"));
    });
});

/**
 * @接口名称 删除一级评论接口
 * @参数 { id: number }
 */
router.delete('/post-comment', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /post-comment 接口`);
    // 获取评论id
    let uid = req.jwtResult.data.uid;
    let postComment = new ObjId(req.body, 'id');
    let flag = postComment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    // 请求业务层
    communityService.deleteComment(uid, postComment.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("操作失败"));
    });
});

/**
 * @接口名称 删除二级评论接口
 * @参数 { id: number }
 */
router.delete('/post-comment-reply', async function (req, res, next) {
    console.log(`[log] [req-id=${req.reqId}] 进入了 /post-comment-reply 接口`);
    let uid = req.jwtResult.data.uid;
    let postComment = new ObjId(req.body, 'id');
    let flag = postComment.check();
    if (!flag.success) {
        return res.status(400).json(flag);
    }
    console.log(`[log] [req-id=${req.reqId}] 参数校验成功`);
    // 请求业务层
    communityService.deleteCommentReply(uid, postComment.id).then((result) => {
        console.log(`[log] [req-id=${req.reqId}] 响应 result`, result);
        res.status(result.code).json(result);
    }).catch((err) => {
        console.log(`[log] [req-id=${req.reqId}] 服务器错误`, err);
        res.status(500).json((new Response()).fail("操作失败"));
    });
});

module.exports = router;
